﻿using LPY.LeHealth.DAL;
using LPY.LeHealth.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LPY.LeHealth.Service
{

    using AutoMapper;
    using AutoMapper.Execution;
    using AutoMapper.Configuration;
    using AutoMapper.Mappers;

    using LPY.LeHealth.DTO;

    public class UserService
    {
        private UserDAL dal;
        public UserService()
        {
            dal = new UserDAL();
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        public UserDTO Login(string name,string pwd)
        {
            var usr= dal.Get(name, pwd);
            return usr;
        }

        public UserDTO Login(string phone)
        {
            var usr = dal.GetByPhoneNumber(phone);
            return usr;
        }

        public UserDTO AdminLogin(string name,string pwd)
        {
            var usr = dal.Get(name, pwd, 2);
            return usr;
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="usr"></param>
        /// <param name="pwd"></param>
        /// <param name="phone"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public bool  Register(string name,string pwd,string phone)
        {
            var usrDto = new UserDTO();
            usrDto.Name = name;
            usrDto.Password = pwd;
            usrDto.PhoneNumber = phone;
            return dal.Insert(usrDto)>0;
        }

        /// <summary>
        /// 通过手机号获取用户
        /// </summary>
        /// <param name="phone">手机号</param>
        /// <returns></returns>
        public UserDTO GetByPhoneNumber(string phone)
        {
            return dal.GetByPhoneNumber(phone);
        }

        /// <summary>
        /// 通过手机号和密码登录
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public UserDTO GetByPhoneAndPwd(string phone,string pwd)
        {
            return dal.GetByPhoneAndPwd(phone, pwd);
        }


        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="usr"></param>
        /// <returns>返回用户Id</returns>
        public long Add(UserDTO usr)
        {
            return dal.Add(usr);
        }

        /// <summary>
        /// 根据用户Id获取用户信息
        /// </summary>
        /// <param name="uid">用户Id</param>
        /// <returns></returns>
        public UserDTO GetById(int uid)
        {
            return dal.GetById(uid);
        }
        /// <summary>
        /// 修改用户的姓名
        /// </summary>
        /// <param name="uid">用户Id</param>
        /// <returns></returns>
        public bool GetByname(UserDTO user)
        {
            return dal.UpdateUserByUid(user);
        }
        /// <summary>
        /// 修改用户昵称
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public bool UpdateName(int Id, string name)
        {
            return dal.UpdateName(Id, name);
        }
        /// <summary>
        /// 修改用户名称
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public bool UpdateTrueName(int Id, string name)
        {
            return dal.UpdateTrueName(Id, name);
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="orderId"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public bool Updapwd(int Id, string Password)
        {
            return dal.Updapwd(Id, Password);
        }
    }
}
